package com.banmoon.observer;

import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

@Getter
public class WeatherBureau implements Subject{

    private int temperature;

    private int pressure;

    private int humidity;

    private List<Observer> observerList;

    public WeatherBureau() {
        this.observerList = new ArrayList<>();
    }

    public void setData(int temperature, int pressure, int humidity){
        System.out.println("气象局天气数据更新");
        this.temperature = temperature;
        this.pressure = pressure;
        this.humidity = humidity;
        // 通知
        notifyAllObserver();
    }

    @Override
    public void addObserver(Observer observer) {
        this.observerList.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        this.observerList.remove(observer);
    }

    @Override
    public void notifyAllObserver() {
        System.out.println("正在通知所有观察者");
        this.observerList.forEach(item -> item.update(this.temperature, this.pressure, this.humidity));
    }
}
